<?php
class User extends AppModel {
	public $name 			= 'User';
	public $displayField 	= 'email';
	public $belongsTo 		= 'Timezone'; 
	public $validate 		= array(
		'username' => array(
			'between' => array(
				'rule' => array('between', 3, 50),
				'message' => 'Username should be between 3 and 50 chars.',
				'required' => TRUE,
				'on' => 'create',
				'last' => true,
			),
			'isUnique' => array(
				'rule' => array('isUnique'),
				'message' => 'Username already taken.',
				'required' => TRUE,
				'on' => 'create',
			),
			'betweenUp' => array(
				'rule' => array('between', 3, 50),
				'message' => 'Username should be between 3 and 50 chars.',
				'on' => 'update',
				'last' => true,
			),
			'isUniqueUp' => array(
				'rule' => array('isUnique'),
				'message' => 'Username already taken.',
				'on' => 'update',
			),
		),
		'password' => array(
			'checkSimplePassword' => array(
				'rule' => array('minLength', 6),
				'message' => 'Password must be at least 6 chars long.',
				'required' => TRUE,
				'last' => true,
				'on' => 'create',
			),
			'checkAdvancedPassword' => array(
				'rule' => 'checkAdvancedPassword',
				'message' => 'Password must contain alpha numeric chars and special chars.',
				'required' => TRUE,
				'last' => true,
				'on' => 'create',
			),
			'match' => array(
				'rule' => 'matchPasswords',
				'message' => 'Your passwords don\'t match.',
				'required' => TRUE,
				'on' => 'create',
			),
			'checkSimplePasswordUp' => array(
				'rule' => array('minLength', 6),
				'message' => 'Password must be at least 6 chars long.',
				'last' => true,
				'on' => 'update',
			),
			'checkAdvancedPasswordUp' => array(
				'rule' => 'checkAdvancedPassword',
				'message' => 'Password must contain alpha numeric chars and special chars.',
				'last' => true,
				'on' => 'update',
			),
			'matchUp' => array(
				'rule' => 'matchPasswords',
				'message' => 'Your passwords don\'t match.',
				'last' => true,
				'on' => 'update',
			),
			'checkOldUp' => array(
				'rule' => 'checkOldPassword',
				'message' => 'Your old password is incorect.',
				'on' => 'update',
				'last' => true
			),
		),
		'first_name' => array(
			'between' => array(
				'rule' => array('between', 3, 50),
				'message' => 'First name  must be between 3 and 50 chars.',
			),
		),
		'last_name' => array(
			'between' => array(
				'rule' => array('between', 3, 50),
				'message' => 'Last name  must be between 3 and 50 chars.',
			),
		),
		'email' => array(
			'email' => array(
				'rule' => array('email'),
				'message' => 'Invalid e-mail address.',
				'required' => TRUE,
				'on' => 'create',
				'last' => true
			),
			'isUnique' => array(
				'rule' => array('isUnique'),
				'message' => 'Email already taken.',
				'required' => TRUE,
				'on' => 'create',
			),
			'emailUp' => array(
				'rule' => array('email'),
				'message' => 'Invalid e-mail address.',
				'on' => 'update',
				'last' => true
			),
			'isUniqueUp' => array(
				'rule' => array('isUnique'),
				'message' => 'Email already taken.',
				'on' => 'update',
			),
		),
		'timezone_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'message' => 'Please select a timezone.',
			),
		),
	);

	public function hashPasswords($data) {
		if (isset($this->data['User']['password'])) {
			$this->data['User']['password'] = Security::hash($this->data['User']['password'], NULL, TRUE);
			return $data;
		}
		return $data;
	}

	public function checkAdvancedPassword($data) {
		if (Configure::read('User.useComplexPassword')) {
			$pattern = '/(?=^.{6,}$)(?=.*\d)(?=.*\W+)(?![.\n])(?=.*[a-zA-Z]).*$/';
			if ( ! preg_match($pattern,$data['password']) ) {
			    return FALSE;
			} else {
			    return TRUE;
			}
		} else {
			return TRUE;
		}
	}
	
	public function matchPasswords($data) {
		if ($data['password'] == $this->data['User']['password_confirm']) {
			return TRUE;
		}
		$this->invalidate('password_confirm', $this->validate['password']['match']['message']);
		return FALSE;
	}
	
	public function checkOldPassword($data) {
		$oldPass = Security::hash($this->data['User']['old_password'], NULL, TRUE);
		$user = $this->find(array('password'=>$oldPass, 'active'=>'1', $this->name.'.id'=>$this->data['User']['id']));
		if ($user) {
			return TRUE;
		}
		$this->invalidate('old_password', $this->validate['password']['checkOldUp']['message']);
		return FALSE;
	}
	
	public function generatePassword($length = 8)
    {
        $password = "";
        $i = 0;
        $possible = "0123456789bcdfghjkmnpqrstvwxyz"; 
 
        while ($i < $length) {
            $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
            if (!strstr($password, $char)) { 
                $password .= $char;
                $i++;
            }
        }
        return $password;
    }
	
	public function beforeSave(){
		$this->hashPasswords(NULL, TRUE);
		return TRUE;
	}

}
?>